home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_peepholer.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  5KB  |  233 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import dis
  5. import sys
  6. from cStringIO import StringIO
  7. import unittest
  8.  
  9. def disassemble(func):
  10.     f = StringIO()
  11.     tmp = sys.stdout
  12.     sys.stdout = f
  13.     dis.dis(func)
  14.     sys.stdout = tmp
  15.     result = f.getvalue()
  16.     f.close()
  17.     return result
  18.  
  19.  
  20. def dis_single(line):
  21.     return disassemble(compile(line, '', 'single'))
  22.  
  23.  
  24. class TestTranforms(unittest.TestCase):
  25.     
  26.     def test_unot(self):
  27.         
  28.         def unot(x):
  29.             if not x == 2:
  30.                 del x
  31.             
  32.  
  33.         asm = disassemble(unot)
  34.         for elem in ('UNARY_NOT', 'JUMP_IF_FALSE'):
  35.             self.assert_(elem not in asm)
  36.         
  37.         for elem in ('JUMP_IF_TRUE', 'POP_TOP'):
  38.             self.assert_(elem in asm)
  39.         
  40.  
  41.     
  42.     def test_elim_inversion_of_is_or_in(self):
  43.         for line, elem in (('not a is b', '(is not)'), ('not a in b', '(not in)'), ('not a is not b', '(is)'), ('not a not in b', '(in)')):
  44.             asm = dis_single(line)
  45.             self.assert_(elem in asm)
  46.         
  47.  
  48.     
  49.     def test_none_as_constant(self):
  50.         
  51.         def f(x):
  52.             return x
  53.  
  54.         asm = disassemble(f)
  55.         for elem in ('LOAD_GLOBAL',):
  56.             self.assert_(elem not in asm)
  57.         
  58.         for elem in ('LOAD_CONST', '(None)'):
  59.             self.assert_(elem in asm)
  60.         
  61.  
  62.     
  63.     def test_while_one(self):
  64.         
  65.         def f():
  66.             while None:
  67.                 pass
  68.             return list
  69.  
  70.         asm = disassemble(f)
  71.         for elem in ('LOAD_CONST', 'JUMP_IF_FALSE'):
  72.             self.assert_(elem not in asm)
  73.         
  74.         for elem in ('JUMP_ABSOLUTE',):
  75.             self.assert_(elem in asm)
  76.         
  77.  
  78.     
  79.     def test_pack_unpack(self):
  80.         for line, elem in (('a, = a,', 'LOAD_CONST'), ('a, b = a, b', 'ROT_TWO'), ('a, b, c = a, b, c', 'ROT_THREE')):
  81.             asm = dis_single(line)
  82.             self.assert_(elem in asm)
  83.             self.assert_('BUILD_TUPLE' not in asm)
  84.             self.assert_('UNPACK_TUPLE' not in asm)
  85.         
  86.  
  87.     
  88.     def test_folding_of_tuples_of_constants(self):
  89.         for line, elem in (('a = 1,2,3', '((1, 2, 3))'), ('("a","b","c")', "(('a', 'b', 'c'))"), ('a,b,c = 1,2,3', '((1, 2, 3))'), ('(None, 1, None)', '((None, 1, None))'), ('((1, 2), 3, 4)', '(((1, 2), 3, 4))')):
  90.             asm = dis_single(line)
  91.             self.assert_(elem in asm)
  92.             self.assert_('BUILD_TUPLE' not in asm)
  93.         
  94.         
  95.         def crater():
  96.             (~[
  97.                 0,
  98.                 1,
  99.                 2,
  100.                 3,
  101.                 4,
  102.                 5,
  103.                 6,
  104.                 7,
  105.                 8,
  106.                 9,
  107.                 0,
  108.                 1,
  109.                 2,
  110.                 3,
  111.                 4,
  112.                 5,
  113.                 6,
  114.                 7,
  115.                 8,
  116.                 9,
  117.                 0,
  118.                 1,
  119.                 2,
  120.                 3,
  121.                 4,
  122.                 5,
  123.                 6,
  124.                 7,
  125.                 8,
  126.                 9,
  127.                 0,
  128.                 1,
  129.                 2,
  130.                 3,
  131.                 4,
  132.                 5,
  133.                 6,
  134.                 7,
  135.                 8,
  136.                 9,
  137.                 0,
  138.                 1,
  139.                 2,
  140.                 3,
  141.                 4,
  142.                 5,
  143.                 6,
  144.                 7,
  145.                 8,
  146.                 9,
  147.                 0,
  148.                 1,
  149.                 2,
  150.                 3,
  151.                 4,
  152.                 5,
  153.                 6,
  154.                 7,
  155.                 8,
  156.                 9,
  157.                 0,
  158.                 1,
  159.                 2,
  160.                 3,
  161.                 4,
  162.                 5,
  163.                 6,
  164.                 7,
  165.                 8,
  166.                 9,
  167.                 0,
  168.                 1,
  169.                 2,
  170.                 3,
  171.                 4,
  172.                 5,
  173.                 6,
  174.                 7,
  175.                 8,
  176.                 9,
  177.                 0,
  178.                 1,
  179.                 2,
  180.                 3,
  181.                 4,
  182.                 5,
  183.                 6,
  184.                 7,
  185.                 8,
  186.                 9,
  187.                 0,
  188.                 1,
  189.                 2,
  190.                 3,
  191.                 4,
  192.                 5,
  193.                 6,
  194.                 7,
  195.                 8,
  196.                 9],)
  197.  
  198.  
  199.     
  200.     def test_elim_extra_return(self):
  201.         
  202.         def f(x):
  203.             return x
  204.  
  205.         asm = disassemble(f)
  206.         self.assert_('LOAD_CONST' not in asm)
  207.         self.assert_('(None)' not in asm)
  208.         self.assertEqual(asm.split().count('RETURN_VALUE'), 1)
  209.  
  210.  
  211.  
  212. def test_main(verbose = None):
  213.     import sys
  214.     test_support = test_support
  215.     import test
  216.     test_classes = (TestTranforms,)
  217.     test_support.run_unittest(*test_classes)
  218.     if verbose and hasattr(sys, 'gettotalrefcount'):
  219.         import gc as gc
  220.         counts = [
  221.             None] * 5
  222.         for i in xrange(len(counts)):
  223.             test_support.run_unittest(*test_classes)
  224.             gc.collect()
  225.             counts[i] = sys.gettotalrefcount()
  226.         
  227.         print counts
  228.     
  229.  
  230. if __name__ == '__main__':
  231.     test_main(verbose = True)
  232.  
  233.